package com.hkts.naturalgas.base.controller;

import com.hkts.naturalgas.base.entity.RoastingDesulphurizing;
import com.hkts.naturalgas.base.service.ICalcinationDesulfurizationDenitrationService;
import com.hkts.naturalgas.utils.ExcelPortUtil;
import com.hkts.naturalgas.utils.Result;
import com.hkts.naturalgas.utils.ResultUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.omg.CORBA.Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassNameExportController
 * @Description
 * @Author yck
 * @Date2021-06-09 15:40
 * @Version V1.0
 **/
@RestController
@RequestMapping("/instrumentdata/export")
public class ExportController {
    @Autowired
    public ICalcinationDesulfurizationDenitrationService iCalcinationDesulfurizationDenitrationService;
    @ResponseBody
    @GetMapping("/exportData")
    public Result exportData(HttpServletRequest request, HttpServletResponse response){
        iCalcinationDesulfurizationDenitrationService.exportData(request,response);
        return null;

    }
    @ResponseBody
    @GetMapping("/exportTime")
    public Result exportTime(HttpServletRequest request, HttpServletResponse response){
        ExcelPortUtil.excelPortTime(response);
        return null;

    }
    /** 功能描述:
    *
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-20 9:16
    * @Description //TODO  处理文件中excle时间间隔大于一分钟的
    */
    @PostMapping("/getTimeDifference")
    @Transactional
    public Result<List<String>> updateByImport(String url) throws Exception {
        File file=new File(url);
        File[] files=file.listFiles();
        List<String> lists=new ArrayList<String>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        for(int i=0;i<files.length;i++){
            File nextfile=  files[i];
            System.out.println(nextfile.getName());
            HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(nextfile));
            HSSFSheet sheet = wb.getSheetAt(0);
            List<RoastingDesulphurizing> list=new ArrayList<RoastingDesulphurizing>(20000);
            for (int j=5;j  >= 5&&j<sheet.getLastRowNum()-5; j ++) {
                HSSFRow row1 = sheet.getRow(j);
                Date dateCellValue = row1.getCell(0).getDateCellValue();
                HSSFRow row2 = sheet.getRow(j+1);
                Date dateCellValue2 = row2.getCell(0).getDateCellValue();
                long between = (dateCellValue2.getTime() - dateCellValue .getTime())/1000;
                long min = between/60;
                format.format(dateCellValue);
                format.format(dateCellValue2);
                int z=lists.size()+1;
                String str=z+":  "+format.format(dateCellValue)+" "+format.format(dateCellValue2)+"差"+min+"分钟";
                if(min>1&&!list.contains(str)){
                    lists.add(str);
                }
            }
           // iRoastingDesulphurizingService.saveOrUpdateBatch(list);
        }

        return ResultUtil.data(lists);
    }

}
